package com.silong.swing.myWindows;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by 龙哥 on 2022-01-17 15:38
 * Silong Software.
 * 文本框与密码框
 */

public class TextFrame extends JFrame {

    JTextField txtUserName; //文本输入框
    JPasswordField txtPassword; //密码输入框
    JButton btnLogin;//登录按钮
    JButton btnCancel;//关闭退出
    JLabel lblUserName; //用户名
    JLabel lblPassword; //密码

    public TextFrame(){
        init();
    }

    private void init(){

        txtUserName = new JTextField("");
        txtPassword = new JPasswordField("");
        btnLogin = new JButton("登录");
        btnCancel = new JButton("关闭");
        lblUserName = new JLabel("用户名称：");
        lblPassword = new JLabel("输入密码：");

        Container window = this.getContentPane();
        this.setSize(500, 300);
        this.setLocationRelativeTo(null); //距中
        this.setTitle("自定义控件大小位置");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //退出窗口关闭程序

        //设置布局为空，优点:摆放灵活，缺点：代码量大，自适应能力差。
        this.setLayout(null);

        //自定义按钮的位置 ，大小
        lblUserName.setLocation(100,50);
        lblUserName.setSize(130,30);
        lblUserName.setFont(new Font("宋体", Font.BOLD, 20));

        txtUserName.setLocation(200,50); //设置按钮位置
        txtUserName.setSize(200,30); //设置按钮大小
        txtUserName.setFont(new Font("宋体", Font.BOLD, 20));

        lblPassword.setLocation(100,100);
        lblPassword.setSize(130,30);
        lblPassword.setFont(new Font("宋体", Font.BOLD, 20));

        txtPassword.setBounds(200, 100, 200, 30); //一次性设置位置与大小
        //设置密码的回显字符
        txtPassword.setEchoChar('@');
        txtPassword.setFont(new Font("Arial", Font.BOLD, 20));

        btnLogin.setFont(new Font("宋体", Font.PLAIN, 20));
        btnCancel.setFont(new Font("宋体", Font.PLAIN, 20));
        btnLogin.setSize(80,40);
        btnCancel.setSize(80,40);
        btnLogin.setLocation(200, 150);
        btnCancel.setLocation(320,150);

        //在窗体上加载文本框
        window.add(txtUserName);
        window.add(txtPassword);
        window.add(lblUserName);
        window.add(lblPassword);
        window.add(btnCancel);
        window.add(btnLogin);

        //添加登录按钮的监听事件
        btnLogin.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String userName = txtUserName.getText();//给出用户名
                String userPassword = new String(txtPassword.getPassword()); //给出密码返回 char[]
                //此时进行登录较验
                System.out.println("登录用户名：" + userName);
                System.out.println("密码：" + userPassword);
            }
        });

        //解决用户按回车键进入下一页
        txtUserName.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                txtPassword.requestFocus(); //密码框获取焦点
            }
        });

        //解决密码框按回车键进入登录功能
        txtPassword.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                btnLogin.doClick(); //登录按钮 Click 事件
            }
        });

    }

    public static void main(String[] args) {
        TextFrame textFrame = new TextFrame();
        textFrame.setVisible(true);
    }
}
